//
//  Account.m
//  Weibo
//
//  Created by wpapple on 16/6/2.
//  Copyright © 2016年 红红火火恍恍惚惚嘿嘿嘿. All rights reserved.
//

#import "Account.h"

@implementation Account

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.access_token forKey:@"access_token"];
    [aCoder encodeObject:self.uid forKey:@"uid"];
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super init];
    if (self) {
        self.access_token = [coder decodeObjectForKey:@"access_token"];
        self.uid = [coder decodeObjectForKey:@"uid"];
    }
    return self;
}

-(BOOL)save{
    
    NSString * accountPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    accountPath = [accountPath stringByAppendingPathComponent:@"account.archiver"];
    
    return [NSKeyedArchiver archiveRootObject:self toFile:accountPath];
    
}

+(instancetype)accountFromSandbox{
    
    NSString * accountPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    accountPath = [accountPath stringByAppendingPathComponent:@"account.archiver"];
    
    Account * account = [NSKeyedUnarchiver unarchiveObjectWithFile:accountPath];
    
    return account;
    
}

+(void)saveLoginStatus{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:@"logined" forKey:@"kLogined"];
    [userDefault synchronize];

}

+(BOOL)isLogin{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    NSString * loginStatus = [userDefault objectForKey:@"kLogined"];
    return [loginStatus isEqualToString:@"logined"];
}

@end
